﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PlaceFinder.BL.Interfaces;
using PlaceFinder.BL;
using PlaceFinder.BO;
using PlaceFinder.UI.Common;

namespace PlaceFinder.UI
{
    /// <summary>
    /// Interaction logic for frmMain.xaml
    /// </summary>
    public partial class frmMain : Window
    {

        private Dictionary<string, Place> _placePolygons = null;
        private string _polygonColor = Constants.POLYGON_COLOR;
        private string _polygonHoverColor = Constants.POLYGON_HOVER_COLOR;

        #region METHODS
        public frmMain()
        {
            
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            LoadFloors();
            LoadCategories();

            LoadPlacesByFloor();
        }

        

        private void LoadFloors()
        {
            IFloorBL bl = new FloorBL();
            cbFloor.DataContext = bl.GetAll();
            cbFloor.DisplayMemberPath = "Description";
            cbFloor.SelectedValuePath = "Number";
            cbFloor.SelectedValue = "1";
        }

        private void LoadCategories()
        {
            ICategoryBL bl = new CategoryBL();
            List<Category> list = bl.GetAll();
            list.Insert(0, new Category() { Id = 0, Name = Constants.TEXT_ALL_F });
            cbCategory.DataContext = list;
            cbCategory.DisplayMemberPath = "Name";
            cbCategory.SelectedValuePath = "Id";
            cbCategory.SelectedValue = "0";
        }

        private void LoadPlacesByFloor()
        {
            IPlaceBL placeBL = new PlaceBL();
            _placePolygons = new Dictionary<string, Place>();
            foreach (Place place in placeBL.GetByFloor(Convert.ToInt32(cbFloor.SelectedValue)))
            {
                if (place.Coordinates != null && place.Coordinates.Count > 0)
                {
                    DrawPlacePolygon(place);
                }
            }
        }

        private void SetPlaceData(Place place)
        {
            if (place.Coordinates != null && place.Coordinates.Count>0)
                DrawPlacePolygon(place);
        }

        private void DrawPlacePolygon(Place place)
        {
            Polygon polygon = new Polygon();
            polygon.Name = "P_"+place.Id;
            _placePolygons.Add(polygon.Name, place);
            polygon.Stroke = Brushes.Purple;
            polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(_polygonColor));
                       
            polygon.Opacity = 0.5;
            foreach (PlacePixel placepixel in place.Coordinates)
            {
                polygon.Points.Add( new Point(placepixel.X, placepixel.Y));
            }
            
            polygon.MouseEnter += new MouseEventHandler(Polygon_MouseEnter);
            polygon.MouseLeftButtonUp += new MouseButtonEventHandler(Polygon_MouseLeftButtonUp);
            polygon.MouseLeave += new MouseEventHandler(Polygon_MouseLeave);
            polygon.MouseMove += new MouseEventHandler(Polygon_MouseMove);
            cvMap.Children.Add(polygon);
        }

        private void DisablePolygon(Polygon polygon)
        {
            polygon.Visibility = Visibility.Hidden;
        }

        private void EnablePolygon(Polygon polygon)
        {
            polygon.Visibility = Visibility.Visible;
        }

        

        

        #endregion 


        #region EVENTS
        private void Polygon_MouseLeave(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(_polygonColor));
            polygon.Cursor = Cursors.Arrow;
            toolTip.Hide();
        }

        
        private void Polygon_MouseMove(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            toolTip.Show(e.GetPosition(cvMap));
        }

        private void Polygon_MouseEnter(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(_polygonHoverColor));
            polygon.Cursor = Cursors.None;
            toolTip.Title = _placePolygons[polygon.Name].Name;
            toolTip.Description = _placePolygons[polygon.Name].Description;            
        }

        private void Polygon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Polygon polygon = ((Polygon)sender); 
            MessageBox.Show("Click en el polygon "+ polygon.Name);
        }

        private void viewRectTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void cbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Polygon polygon = null;
            int categoryId = Convert.ToInt32(cbCategory.SelectedValue);
            _polygonColor = Constants.POLYGON_COLOR;
            _polygonHoverColor = Constants.POLYGON_HOVER_COLOR;
            if (categoryId != 0)
            {
                _polygonColor = Constants.CATEGORY_COLOR;
                _polygonHoverColor = Constants.CATEGORY_HOVER_COLOR;
            }
            foreach (var item in cvMap.Children)
            {
                if (item is Polygon)
                {
                    polygon = (Polygon)item;
                    DisablePolygon(polygon);
                    polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(_polygonColor));
                    if (categoryId == 0 || _placePolygons[polygon.Name].CategoryId == categoryId)
                    {
                        EnablePolygon(polygon);
                    }
                }
            }

        }

        #endregion
    }
}
